package com.pack.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.pack.ToolEntity.BaseController;
import com.pack.ToolEntity.BpmUtils;
import com.pack.bean.User;
import com.pack.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController{
	    @Autowired
	    private UserService userService ;

	    @RequestMapping(value = "/first", method = RequestMethod.GET)
	    public String test1() {
	    	User user = new User();
	    	user.setName("123");
			List<User> users = userService.findPermissionList(user);
			Set<String> set = new HashSet<String>();
			if(BpmUtils.listIsNull(users)){
				for (User userss : users) {
					set.add(userss.getPermissionName());
					System.out.println(userss.getPermissionName()+"------------------");
				}
			}
			return "Login";
	    }

        @RequestMapping(value = "/tree", method = RequestMethod.GET)
		@RequiresPermissions("teacher")
        public  String tree()
        {
            return "angular";
        }

		@RequestMapping(value = "/student", method = RequestMethod.GET)
		public  String student()
		{
			return "tree";
		}

		@RequestMapping(value = "/teacher", method = RequestMethod.GET)
		public  String teacher()
		{
			return "tree";
		}

		@RequestMapping(value = "/login", method = RequestMethod.GET)
		public @ResponseBody String login(User user)
		{
			System.out.println("-----------------"+user.getName());
			System.out.println("-------------------"+user.getPassword());
			Subject sub = SecurityUtils.getSubject();
			UsernamePasswordToken userLogin = new UsernamePasswordToken(user.getName(),user.getPassword());
			try {
				sub.login(userLogin);
				return "1";
			}catch (Exception e){
				return "2";
			}
		}
}
